<?php


	namespace Midi\Event;
	
	use \Midi\Event;
	use \Midi\Util\Util;

	abstract class MetaEvent implements Parameterized, Event {
		
		protected $length;
		
		protected $data;
		
		public function __construct($data = null) {
			if (is_string($data)) {
				$this->length = strlen($data);
			} else if (is_array($data)) {
				$this->length = count($data);
			} else {
				$this->length = 0;
			}
			
			$this->data = $data;
		}
		
		public function getLength() {
			$lengthOfDelta = strlen(Util::getDeltaByteSequence($this->length));
			
			return 1 + 1 + $lengthOfDelta + $this->length;
		}
		
		public function __toString() {
			return MetaEventType::getEventTypeName($this->getSubtype()) . ': ' . $this->getParamDescription();
		}
		
		public function toBinary() {
			if (is_array($this->data)) {
				$data = '';
				foreach ($this->data as $datum) {
					$data .= Util::pack($datum);
				}
			} else {
				$data = $this->data;
			}
			
			return
				Util::pack($this->getType(), $this->getSubtype()) .
				Util::getDeltaByteSequence($this->length) .
				$data;
		}
		
		public function getData() {
			return array(
				$this->getSubtype(),
				$this->length,
				$this->data
			);
		}
		
		public function getType() {
			return EventType::META;
		}
		
		public abstract function getSubtype();
		
	}

?>